package com.jy.adscm.operation.cwb.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jy.adscm.operation.cwb.bean.CwIedetailinfo;
import com.jy.adscm.operation.cwb.service.CwIedetailinfoService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yyy
 * @since 2019-01-08
 */
@RestController
@RequestMapping("adscm/operation/cwIedetailinfo")
public class CwIedetailinfoController {

    @Autowired
    CwIedetailinfoService cwIedetailinfoService;
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    SimpleDateFormat sd=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @RequestMapping("/insert")
    public SimpleCoreResponse insert(CwIedetailinfo cw, HttpServletRequest request,String data){
        JSONObject jsonObject = JSON.parseObject(data);
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        cw.setIedid(UUID.randomUUID().toString());
        cw.setTxr(token.getUser().getId().toString());
        cw.setIednr(jsonObject.getString("iednr"));
        if ("".equals(jsonObject.getString("iedsj"))){
            cw.setIedsj(null);
        }else {
            cw.setIedsj(jsonObject.getString("iedsj"));
        }
       cw.setIeid(jsonObject.getString("ieid"));
        return new SimpleCoreResponse("200","新增成功",cwIedetailinfoService.insert(cw));
    }
    @RequestMapping("/update")
    public  SimpleCoreResponse update(CwIedetailinfo cw,String data){
        JSONObject jsonObject = JSON.parseObject(data);
        cw.setIednr(jsonObject.getString("iednr"));
        if ("".equals(jsonObject.getString("iedsj"))){
            cw.setIedsj(null);
        }else {
            cw.setIedsj(jsonObject.getString("iedsj"));
        }
        cw.setIedid(jsonObject.getString("iedid"));
        return new SimpleCoreResponse("200","修改成功",cwIedetailinfoService.updateById(cw));
    }
    @RequestMapping("/delete")
    public SimpleCoreResponse delete(String id){
        return new SimpleCoreResponse("200","删除成功",cwIedetailinfoService.deleteById(id));
    }
    @RequestMapping("/queryByIeDid")
    public SimpleCoreResponse queryByIeDid(String iedid){
        return new SimpleCoreResponse("200","查询成功",cwIedetailinfoService.queryByIeId(iedid));
    }
}

